<cfcomponent hint="I get all of the database information names, table and column names" output="false">

	<!---    Date: 1/4/2008 Usage: I initilize the component --->
    <cffunction name="init" output="false" access="public" returntype="dataBaseService" hint="I initilize the component">

	  	<cfset setDataSourceService(
	  			CreateObject("java","coldfusion.server.ServiceFactory").getDataSourceService()) />
	  	<cfset setjdbcTypes() />
	  	<cfreturn this/>
    </cffunction>
	
	<!--- Usage: GetdataSourceService / SetdataSourceService methods for dataSourceService value --->
    <cffunction name="getdataSourceService" access="public" output="false" returntype="any">
       
		<cfreturn variables.instance.dataSourceService />
    </cffunction>

    <cffunction name="setdataSourceService" access="public" output="false" returntype="void">
       <cfargument name="dataSourceService" type="any" required="true" />
       <cfset variables.instance.dataSourceService = arguments.dataSourceService />
    </cffunction>
	
	<!---    Date: 1/4/2008 Usage: I get all of the coldfusion datasource names --->
    <cffunction name="getDataSourceNames" output="false" access="public" returntype="string" hint="I get all of the coldfusion datasource names">
		<cfset var local = StructNew() />
		<cfset local.stc = variables.instance.dataSourceService.getDatasources() />
		<cfset local.returnVal = "[" />
		<cfloop collection="#local.stc#" item="local.item">
			<cfif local.stc[local.item].driver NEQ "Apache Derby Embedded">
				<cfif local.returnVal NEQ "[">
					<cfset local.returnVal = local.returnVal & ","/>
				</cfif>
				<cfset local.returnVal = local.returnVal & "{dsn:'" & local.stc[local.item].name & "'}"/>
			</cfif>
		</cfloop>
		<cfreturn local.returnVal & "]"/>
    </cffunction>
	
	<!--- Usage: Getdsn / Setdsn methods for dsn value --->
    <cffunction name="getdsn" access="public" output="false" returntype="string">
       <cfreturn variables.instance.dsn />
    </cffunction>

    <cffunction name="setdsn" access="public" output="false" returntype="void">
       <cfargument name="dsn" type="string" required="true" />
       <cfset variables.instance.dsn = arguments.dsn />
    </cffunction>
	
	<!--- Usage: GetConnection / SetConnection methods for Connection value --->
    <cffunction name="getConnection" access="public" output="false" returntype="any">
       <cfreturn variables.instance.Connection />
    </cffunction>

    <cffunction name="setConnection" access="public" output="false" returntype="void">
		<cfargument name="Connection" type="any" required="true" />
		<cfset variables.instance.Connection = Connection />
    </cffunction>
	
	<!--- Usage: GetDBMetaData / SetDBMetaData methods for DBMetaData value --->
    <cffunction name="getDBMetaData" access="public" output="false" returntype="any">
       <cfreturn variables.instance.DBMetaData />
    </cffunction>

    <cffunction name="setDBMetaData" access="public" output="false" returntype="void">
       <cfargument name="DBMetaData" type="any" required="true" />
       <cfset variables.instance.DBMetaData = arguments.DBMetaData />
    </cffunction>
	
	<!---    Date: 1/4/2008 Usage: I get all of the table names for a datasource --->
    <cffunction name="getTableNames" output="false" access="public" returntype="string" hint="I get all of the table names for a datasource">
		<cfset var tabletypes = arraynew(1) />
		<cfset var tableNames = "" />
		<cfset var tableNameJSON = "" />
		<cfset arrayappend(tabletypes,"TABLE")/>
		<cfset tableNames = getDBMetaData().getTables(JavaCast('null',''),JavaCast('null',''),'%',tabletypes)/>
		<cfset tableNameJSON = "[">
		<cfloop condition="tableNames.next()">
			<cfif tableNames.getRow()>
				<cfif tableNameJSON NEQ '['>
					<cfset tableNameJSON = tableNameJSON & "," />
				</cfif>
				<cfset tableNameJSON = tableNameJSON & "{tableName:'" & tableNames.getString(JavaCast('string','TABLE_NAME')) & "'}"/>
			</cfif>
		</cfloop>
		<cfreturn tableNameJSON & "]">		
    </cffunction>
	
	<!---    Date: 1/4/2008 Usage: I get all of the column information for a table passed in --->
    <cffunction name="getColumnInfo" output="false" access="public" returntype="string" hint="I get all of the column information for a table passed in">
		<cfargument name="tablename" default="" hint="I am the table name that we are going to get the column information for.">
		<cfargument name="columnname" default="%" hint="I am the column name that you want to get info for.">
		<cfset var metaData = getDBMetaData().getColumns(JavaCast('null',''),JavaCast('null',''),'#arguments.tableName#','#arguments.columnName#') />		
		<cfset var returnValue = "[" />

		<cfloop condition="metaData.next()">
			<cfif metaData.getRow()>
				<cfif returnValue NEQ "[">
					<cfset returnValue = returnValue & ","/>
				</cfif>
				<cfset returnValue = returnValue & "{columnName:'#metaData.getString(JavaCast('string','COLUMN_NAME'))#'"/>
				<cfif metaData.getString(JavaCast('string','COLUMN_SIZE')) NEQ "">
					<cfset returnValue = returnValue & ", columnLen:" & metaData.getString(JavaCast('string','COLUMN_SIZE'))/>
				<cfelse>
					<cfset returnValue = returnValue & ", columnLen:100" />					
				</cfif>
				<cfset returnValue = returnValue & ", columnType:'cf_sql_" & getjdbcType(metaData.getString(JavaCast('string','DATA_TYPE'))) & "'}"/>
			</cfif>
		</cfloop>
		<cfreturn returnValue & "]"/>
    </cffunction>
	
	<cffunction name="setjdbcTypes" output="false" returntype="void"  hint="I create a struct of the jdbc types">
		<cfset var sqltype = createobject("java","java.sql.Types") />
		<cfset var idx = "" />
		<cfset variables.instance.jdbcType = structnew() />
		
		<cfloop item="idx" collection="#sqltype#">
			<cfset variables.instance.jdbcType[idx] = LCase(sqltype[idx]) />
			<cfset variables.instance.jdbcType[sqltype[idx]] = LCase(idx) />
		</cfloop>
	</cffunction>
	
	<cffunction name="getjdbcType" output="false" returntype="string"  hint="I get a jdbc type.">
		<cfargument default="" name="jdbcType">
		
		<cftry>
			<cfreturn variables.instance.jdbcType[arguments.jdbctype] />
			<cfcatch type="any">
				<cfreturn 'none'/>
			</cfcatch>
		</cftry>
	</cffunction>

	<cffunction name="getCFSQLTypes" output="false" returntype="array" hint="I return an array of cf_sql types">
		<cfset var arrCFSQLTypes = arrayNew(1) />
		<cfscript>
			arrCFSQLTypes[1] = "cf_sql_bigint";
			arrCFSQLTypes[2] = "cf_sql_bit";
			arrCFSQLTypes[3] = "cf_sql_char";
			arrCFSQLTypes[4] = "cf_sql_blob";
			arrCFSQLTypes[5] = "cf_sql_clob";
			arrCFSQLTypes[6] = "cf_sql_date";
			arrCFSQLTypes[7] = "cf_sql_decimal";
			arrCFSQLTypes[8] = "cf_sql_double";
			arrCFSQLTypes[9] = "cf_sql_float";
			arrCFSQLTypes[10] = "cf_sql_idstamp";
			arrCFSQLTypes[11] = "cf_sql_integer";
			arrCFSQLTypes[12] = "cf_sql_longvarchar";
			arrCFSQLTypes[13] = "cf_sql_money";
			arrCFSQLTypes[14] = "cf_sql_money4";
			arrCFSQLTypes[15] = "cf_sql_numeric";
			arrCFSQLTypes[16] = "cf_sql_real";
			arrCFSQLTypes[17] = "cf_sql_refcursor";
			arrCFSQLTypes[18] = "cf_sql_smallint";
			arrCFSQLTypes[19] = "cf_sql_time";
			arrCFSQLTypes[20] = "cf_sql_timestamp";
			arrCFSQLTypes[21] = "cf_sql_tinyint";
			arrCFSQLTypes[22] = "cf_sql_varchar";
		</cfscript>
		<cfreturn arrCFSQLTypes>
	</cffunction>

</cfcomponent>


